class TeacherInfoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def securityService
    def postService

    def show = {

	def currentUser = securityService.getCurrentUser()
        def teacherInfoInstance = Teacher.get(currentUser.id)?.teacherInfo
        
        return [teacherInfoInstance: teacherInfoInstance, author: currentUser, site:Site.findByAuthor(currentUser),classeSites:postService.getClassSiteByCurrentUserClassId(currentUser)]

    }

    def edit = {
        def currentUser = securityService.getCurrentUser()
        def teacherInfoInstance = Teacher.get(currentUser.id)?.teacherInfo
        
        return [teacherInfoInstance: teacherInfoInstance, author: currentUser, site:Site.findByAuthor(currentUser),classeSites:postService.getClassSiteByCurrentUserClassId(currentUser)]
    }

    def update = {
        def currentUser = securityService.getCurrentUser()
	def teacherInfoInstance
        if(!params.id){
		teacherInfoInstance = new TeacherInfo(params)
        }else{
		teacherInfoInstance = TeacherInfo.get(params.id)
		teacherInfoInstance.properties = params
	}
        // todo  后面将要改成session
        Teacher t = Teacher.get(currentUser.id)
        if (teacherInfoInstance.save(flush: true)) {
            flash.message = "信息修改成功"
            t.teacherInfo = teacherInfoInstance
            t.save();
            redirect(action: "show")
        }
        else {
            render(view: "edit", model: [teacherInfoInstance: teacherInfoInstance, author: currentUser, site:Site.findByAuthor(currentUser),classeSites:postService.getClassSiteByCurrentUserClassId(currentUser)])
        }
    }

}
